<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/18  11:58
// +----------------------------------------------------------------------
namespace Common\Model;
use Think\Model;

/*
 * Pay模型
 * 主要针对支付完成的操作
 * */
class PayModel extends Model
{
    function complete_rent($pay_id,$money){
        $Pay = M("Pay");
        $Order_book = M("Order_book");
        $dp = $Pay->where("pay_id=$pay_id")->find();
        if ($dp["status"] == 1) {
            //返回结果-已支付不做处理
            return true;
        }
        $Pay->startTrans();
        $data1['status'] = 1;
        $data1['money_real'] = $money;
        $Pay->where("pay_id=$pay_id")->data($data1)->save();
        $Order_book->where("pay_id=$pay_id")->data("state_order=1")->save();
        $dp = $Pay->where("pay_id=$pay_id")->find();
        $do = $Order_book->where("pay_id=$pay_id")->find();
        if ($dp["status"] == 1 && $do['state_order'] == 1) {

        } else {
            $Pay->rollback();
            return false;
        }
        //更改购书库存数量
        $do = $Order_book->where("pay_id=$pay_id AND type=1")->field("order_id,price_freight,is_freightfree,user_id")->find();
        $order_id = $do["order_id"];
        $Orderhasbook = M("Order_has_book");
        $dbook = $Orderhasbook->where("order_id=$order_id AND type=1")->field("book_id,quantity")->select();
        $Book = M("Book");
        foreach ($dbook as $k => $v) {
            $Book->where("book_id=" . $v["book_id"])->setDec("stock_rent", $v["quantity"]);
        }
        //根据是否免借阅运费，更改会员自身免借阅运费次数
        if ($do['is_freightfree'] == "1") {
            //免借阅运费，减少会员免借阅运费次数
            $Card = M("Card");
            $Card->where("user_id=" . $do['user_id'])->setDec("num_delivery");
        }
        //通知消息
        $Message = D("Message");
        $user_id = $do['user_id'];
        $msg = "【新华书店】您的订单：{$order_id}（订单号）已支付成功。工作人员会尽快发货，请耐心等待~";
        $Message->sendmessage($user_id, $msg);

        $Pay->commit();
        return true;
    }

    function complete_buy($pay_id,$money){
        $Pay = M("Pay");
        $Order_book = M("Order_book");
        $dp = $Pay->where("pay_id=$pay_id")->find();
        if ($dp["status"] == 1) {
            return true;
        }
        $Pay->startTrans();
        //更新支付状态
        $data1['status'] = 1;
        $data1['money_real'] = $money;
        $Pay->where("pay_id=$pay_id")->data($data1)->save();
        $Order_book->where("pay_id=$pay_id")->data("state_order=1")->save();
        $dp = $Pay->where("pay_id=$pay_id")->find();
        $do = $Order_book->where("pay_id=$pay_id")->find();
        if ($dp["status"] == 1 && $do['state_order'] == 1) {

        } else {
            $Pay->rollback();
            return false;
        }
        //更改购书库存数量
        $do = $Order_book->where("pay_id=$pay_id AND type=0")->field("order_id,user_id,price_freight")->find();
        $order_id = $do["order_id"];
        $Orderhasbook = M("Order_has_book");
        $dbook = $Orderhasbook->where("order_id=$order_id AND type=0")->field("book_id,quantity")->select();
        $Book = M("Book");
        foreach ($dbook as $k => $v) {
            $Book->where("book_id=" . $v["book_id"])->setDec("stock_buy", $v["quantity"]);
        }
        //通知消息
        $Message = D("Message");
        $user_id = $do['user_id'];
        $msg = "【新华书店】您的订单：{$order_id}（订单号）已支付成功。工作人员会尽快发货，请耐心等待~";
        $Message->sendmessage($user_id, $msg);

        $Pay->commit();
        return true;
    }
}